﻿using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.DocumentDB
{
    /// <summary>Represents a DB account.</summary>
    public class AccountViewModel : DocumentClientViewModelBase<StudioViewModel>
    {
        /// <summary>Construct a model from account credentials.</summary>
        /// <param name="id">ID of the account.</param>
        /// <param name="authKey">Authorization key to access the account.</param>
        /// <param name="parent">Parent of the model.</param>
        public AccountViewModel(string id, string authKey, StudioViewModel parent)
            : base(id, authKey, parent)
        {
            Id = id;
            AuthKey = authKey;

            DatabaseFolder = new FolderViewModel<DatabaseViewModel, AccountViewModel>(
                this,
                "Databases",
                true,
                DatabaseViewModel.GetLazyDatabases(Client, this));
            Artefacts = new FolderViewModel[]
            {
                DatabaseFolder,
                new FolderViewModel<int, AccountViewModel>(this,"Media", false, LoadableCollection<int>.CreateEmpty())
            }.ToList();
        }

        /// <summary>Id of the account.</summary>
        public string Id { get; private set; }

        /// <summary>Authorization key to access the account.</summary>
        public string AuthKey { get; private set; }

        /// <summary>Artefacts of the account.</summary>
        public List<FolderViewModel> Artefacts { get; private set; }

        /// <summary>Databases of the account.</summary>
        public FolderViewModel<DatabaseViewModel, AccountViewModel> DatabaseFolder { get; private set; }

        /// <summary>Performs a validation the credentials are working.</summary>
        /// <returns><c>true</c> iif the credentials are working.</returns>
        public async Task IsValidAsync()
        {
            await Client.OpenAsync();
        }

        /// <summary>Asynchronously creates a DB under the account.</summary>
        /// <param name="id">DB id.</param>
        /// <returns>Created database.</returns>
        public async Task<DatabaseViewModel> CreateDbAsync(string id)
        {
            var db = new Database { Id = id };

            db = await Client.CreateDatabaseAsync(db);

            var model = new DatabaseViewModel(Client, db, this);

            DatabaseFolder.FolderContent.Add(model);

            return model;
        }

        /// <summary>Delete a database.</summary>
        /// <param name="db">Database to delete</param>
        /// <returns>Asynchronous completion.</returns>
        public async Task DeleteDatabaseAsync(DatabaseViewModel db)
        {
            await Client.DeleteDatabaseAsync(db.SelfLink);

            DatabaseFolder.FolderContent.Remove(db);
        }
    }
}